﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AmkeniCrossLoginApplication.Data_Access;

namespace Amkeni_System.CO.Edit
{
    public partial class AddOutcome : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void SaveOutcomeButton_Click(object sender, EventArgs e)
        {
            try
            {
                int ID = int.Parse(OutcomeIDTextBox.Text.Trim());

                var AnOutcome = (from o in db.programme_outcome where o.outcome_id == ID select o).FirstOrDefault();

                if (AnOutcome == null)
                {
                    //===============================+
                    //  Add a new Outcome Record     |
                    //===============================+
                    programme_outcome newOutcome = new programme_outcome();
                    newOutcome.outcome_id = ID;
                    newOutcome.thematic_area = ThematicAreaTxBx.Text.Trim();
                    newOutcome.name = OutcomeTextBox.Text.Trim();

                    db.programme_outcome.Add(newOutcome);
                    db.SaveChanges();

                    error.Text = "SAVED";
                    error.Visible = true;

                    OutcomeIDTextBox.Text = null;
                    OutcomeTextBox.Text = null;
                }
                else
                {
                    error.Text = "That Programme Outcome ID already exists";
                    error.Visible = true;
                }
            }
            catch (Exception ex)
            {
                error.Text = "Database Error: "+ex.Message+"<br /> "+ex.StackTrace;
                error.Visible = true;
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Outcomes.aspx", true);
        }
    }
}